package com.zzsy.shoppingpro.community.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.community.bean.HotPostBean;
import com.zzsy.shoppingpro.community.utils.DensityUtil;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2019/8/21 0021.
 */

public class HotPostListViewAdapter extends BaseAdapter {

    private Context mContext;

    private List<HotPostBean> mData;

    public HotPostListViewAdapter(Context context, List<HotPostBean> data) {
        this.mContext = context;
        this.mData = data;
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_hotpost_listview, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        HotPostBean hotPostBean = mData.get(position);

        holder.tvHotUsername.setText(hotPostBean.getUsername());
        SimpleDateFormat myFmt = new SimpleDateFormat("MM-dd HH:mm");
        holder.tvHotAddtime.setText(myFmt.format(Integer.parseInt(hotPostBean.getAdd_time())));

        Glide.with(mContext).load(Config.BASE_URl_IMAGE + hotPostBean.getFigure()).into(holder.ivHotFigure);
        holder.tvHotSaying.setText(hotPostBean.getSaying());
        holder.tvHotLikes.setText(hotPostBean.getLikes());
        holder.tvHotComments.setText(hotPostBean.getComments());


        String is_top = hotPostBean.getIs_top();

        if ("1".equals(is_top)) {
            LinearLayout.LayoutParams textViewLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView textView = new TextView(mContext);
            textView.setText("置顶");
            textViewLp.setMargins(DensityUtil.dip2px(mContext, 10), 0, DensityUtil.dip2px(mContext, 5), 0);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundResource(R.drawable.is_top_shape);
            textView.setPadding(DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5));
            holder.llHotPost.removeAllViews();
            holder.llHotPost.addView(textView, textViewLp);
        }
        String is_hot = hotPostBean.getIs_hot();
        if ("1".equals(is_hot)) {
            LinearLayout.LayoutParams textViewLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView textView = new TextView(mContext);
            textViewLp.setMargins(0, 0, DensityUtil.dip2px(mContext, 5), 0);
            textView.setText("热门");
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.WHITE);
            textView.setPadding(DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5));
            textView.setBackgroundResource(R.drawable.is_hot_shape);
            holder.llHotPost.addView(textView, textViewLp);
        }
        String is_essence = hotPostBean.getIs_essence();
        if ("1".equals(is_essence)) {
            LinearLayout.LayoutParams textViewLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            textViewLp.setMargins(0, 0, DensityUtil.dip2px(mContext, 5), 0);
            TextView textView = new TextView(mContext);
            textView.setText("精华");
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.WHITE);
            textView.setPadding(DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5));
            textView.setBackgroundResource(R.drawable.is_essence_shape);
            holder.llHotPost.addView(textView, textViewLp);
        }

        return convertView;
    }

    class ViewHolder {
        TextView tvHotUsername;
        TextView tvHotAddtime;
        ImageView ivHotFigure;
        LinearLayout llHotPost;
        TextView tvHotSaying;
        TextView tvHotLikes;
        TextView tvHotComments;
        ImageButton ibNewPostAvatar;

        public ViewHolder(View itemView) {
            tvHotUsername = itemView.findViewById(R.id.tv_hot_username);
            tvHotAddtime = itemView.findViewById(R.id.tv_hot_addtime);
            ivHotFigure = itemView.findViewById(R.id.iv_hot_figure);
            llHotPost = itemView.findViewById(R.id.ll_hot_post);
            tvHotSaying = itemView.findViewById(R.id.tv_hot_saying);
            tvHotLikes = itemView.findViewById(R.id.tv_hot_likes);
            tvHotComments = itemView.findViewById(R.id.tv_hot_comments);
            ibNewPostAvatar = itemView.findViewById(R.id.ib_new_post_avatar);
        }
    }
}
